
package com.comtop.increment;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IPluginDescriptor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

public class IncrementPlugin extends AbstractUIPlugin {
    
    // The shared instance
    private static IncrementPlugin plugin;
    
    private ResourceBundle resourceBundle;
    
    // The plug-in ID
    public static final String PLUGIN_ID = "increment";
    
    /**
     * The constructor
     */
    public IncrementPlugin(IPluginDescriptor descriptor) {
        super(descriptor);
        plugin = this;
        try {
            resourceBundle = ResourceBundle.getBundle("com.comtop.increment.IncrementPluginResources");
        } catch (MissingResourceException ex) {
            IncrementPlugin.log(ex);
        }
    }
    
    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;
    }
    
    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    @Override
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        super.stop(context);
    }
    
    /**
     * Returns the shared instance
     * 
     * @return the shared instance
     */
    public static IncrementPlugin getDefault() {
        return plugin;
    }
    
    /**
     * Returns an image descriptor for the image file at the given plug-in relative path
     * 
     * @param path the path
     * @return the image descriptor
     */
    public static ImageDescriptor getImageDescriptor(String path) {
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }
    
    public static IWorkspace getWorkspace() {
        return ResourcesPlugin.getWorkspace();
    }
    
    public static String getResourceString(String key) {
        ResourceBundle bundle = getDefault().getResourceBundle();
        String res = null;
        try {
            res = bundle.getString(key);
        } catch (MissingResourceException _ex) {
            res = key;
        }
        return res;
    }
    
    public ResourceBundle getResourceBundle() {
        return resourceBundle;
    }
    
    public static void log(Object msg) {
        ILog log = getDefault().getLog();
        Status status = new Status(1, getDefault().getDescriptor().getUniqueIdentifier(), 4, msg + "\n", null);
        log.log(status);
    }
    
    public static void log(Throwable ex) {
        ILog log = getDefault().getLog();
        StringWriter stringWriter = new StringWriter();
        ex.printStackTrace(new PrintWriter(stringWriter));
        String msg = stringWriter.getBuffer().toString();
        Status status = new Status(4, getDefault().getDescriptor().getUniqueIdentifier(), 4, msg, null);
        log.log(status);
    }
    
    public String getTarget() {
        return getPreferenceStore().getString(PreferenceConstants.P_TARGET);
    }
    
    public String getPreferencePageValue(String key) {
        return getPreferenceStore().getString(key);
    }
    
    public boolean isSupported() {
        String osName = System.getProperty("os.name");
        return osName.indexOf("Windows") != -1 || osName.indexOf("Mac") != -1;
    }
    
}
